﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using PBOffice.BLL;
using PBOffice.DAL;

public partial class MeetingManager_AddMeeting : System.Web.UI.Page
{
    MeetingManagerBLL mb = new MeetingManagerBLL();
    MeetingRoom mr = new MeetingRoom();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlType.DataSource = mb.GetRoomType();
            ddlType.DataTextField="roomTypeId";
            ddlType.DataValueField="roomTypeNo";
            ddlType.DataBind();
            if (Request.QueryString["id"] =="")
            {
                plNo.Visible = false;
                this.imgBookCover.Attributes.Add("style", "display:none;");
            }
          this.BindData();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
       
        mr.RoomId = txtName.Text.Trim();  //名称
        mr.RoomArea =  Convert.ToDecimal(txtArea.Text.Trim());//面积
        mr.RoomPeopleNo =Convert.ToInt32(txtNum.Text.Trim());//人数
        RoomType rt = new RoomType();
        mr.RoomType = rt;
        rt.RoomTypeId = ddlType.SelectedItem.Text;//类型
        mr.RoomEquitpment = txtEqu.Text.Trim(); //装备
        if (fvImage.HasFile)
        {
            string filename=fvImage.FileName;
            if (Path.GetExtension(filename).ToLower() == ".jpg")
            {
                string filenameser = Server.MapPath("~/Images/Room/"+ filename);
                fvImage.PostedFile.SaveAs(filenameser);
                mr.RoomImage = filenameser;
            }
        }

        if (Request.QueryString["id"] == null)
        {
            mb.AddMeetingInfor(mr);
            Page.ClientScript.RegisterClientScriptBlock(typeof(string), "key", "<script>alert('添加成功！')</script>");
        }
        else
        {
            mr.RoomNo=Convert.ToInt32(Request.QueryString["id"]);
            if(mb.UpdateMeetingInfor(mr))
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "key", "<script>alert('修改成功！')</script>");
        }
    }

    private void BindData()
    {
        if (Request.QueryString["id"] != "")
        {
            string id=Request.QueryString["id"];
            int num =Convert.ToInt32(id );
            List<MeetingRoom> mer = mb.GetRoomByNo(num);
            lblNo.Text = id;
            this.txtName.Text=mer[0].RoomId;
            this.txtArea.Text = mer[0].RoomArea.ToString();
            this.txtNum.Text = mer[0].RoomPeopleNo.ToString();
            this.txtEqu.Text = mer[0].RoomEquitpment;
            RoomType rb = new RoomType();
            rb = mer[0].RoomType;
            this.ddlType.SelectedValue = rb.RoomTypeNo.ToString();
            this.imgBookCover.ImageUrl = mer[0].RoomImage;

        }
    }
}